import React, { Component } from 'react'
import { nanoid } from 'nanoid'

export default class Header extends Component {
  handleKeyup = (e) => {
    const { target, keyCode } = e
    if (keyCode !== 13) return
    if (!target.value.trim().length) return
    const todo = {
      id: nanoid(),
      name: target.value.trim(),
      done: false,
    }
    this.props.addTodo(todo)
    target.value = ''
  }

  render() {
    return (
      <div className="todo-header">
        <input
          type="text"
          onKeyUp={this.handleKeyup}
          placeholder="请输入你的任务名称，按回车键确认"
        />
      </div>
    )
  }
}
